package com.dwjm.exam.stu_reg;

import com.dwjm.exam.pojo.Student;
import com.dwjm.utils.ConnectionDatabaseForMySQLUseJdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RegDaoImpl implements RegDao {
    @Override
    public boolean isUsernameExists(String username) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        boolean exists = false;

        try {
            conn = ConnectionDatabaseForMySQLUseJdbc.getConnection();
            String sql = "SELECT 1 FROM student WHERE username = ? LIMIT 1";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                exists = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionDatabaseForMySQLUseJdbc.close(conn, pstmt, rs);
        }

        return exists;
    }

    @Override
    public boolean isStudentIdExists(String studentId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        boolean exists = false;

        try {
            conn = ConnectionDatabaseForMySQLUseJdbc.getConnection();
            String sql = "SELECT 1 FROM student WHERE studentId = ? LIMIT 1";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, studentId);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                exists = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionDatabaseForMySQLUseJdbc.close(conn, pstmt, rs);
        }

        return exists;
    }

    @Override
    public boolean registerStudent(Student student) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        boolean success = false;

        try {
            conn = ConnectionDatabaseForMySQLUseJdbc.getConnection();
            String sql = "INSERT INTO student (studentId, name, gender, birthDate, idCard, college, " +
                    "major, className, status, dormId, bedNumber, phone, email, emergencyContact, " +
                    "username, password) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, student.getStudentId());
            pstmt.setString(2, student.getName());
            pstmt.setString(3, student.getGender());
            pstmt.setDate(4, new java.sql.Date(student.getBirthDate().getTime()));
            pstmt.setString(5, student.getIdCard());
            pstmt.setString(6, student.getCollege());
            pstmt.setString(7, student.getMajor());
            pstmt.setString(8, student.getClassName());
            pstmt.setString(9, student.getStatus());
            pstmt.setString(10, student.getDormId());
            pstmt.setString(11, student.getBedNumber());
            pstmt.setString(12, student.getPhone());
            pstmt.setString(13, student.getEmail());
            pstmt.setString(14, student.getEmergencyContact());
            pstmt.setString(15, student.getUsername());
            pstmt.setString(16, student.getPassword());

            int affectedRows = pstmt.executeUpdate();
            success = affectedRows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionDatabaseForMySQLUseJdbc.close(conn, pstmt, null);
        }

        return success;
    }
}